package com.art.museumbackend.mapper;

import com.art.museumbackend.entity.Articles;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author yezi
 * @since 2024-12-07
 */
@Mapper
public interface ArticlesMapper extends BaseMapper<Articles> {

    // 新增文章
    @Insert("INSERT INTO articles (parent_column_id, sub_column_id, title, source, resume, cover_image_url, video_url, content, state, created_time, updated_time) " +
            "VALUES (#{parentColumnId}, #{subColumnId}, #{title}, #{source}, #{resume}, #{coverImageUrl}, #{videoUrl}, #{content}, #{state}, #{createdTime}, #{updatedTime})")
    @Options(useGeneratedKeys = true, keyProperty = "articleId") // 获取自动生成的ID
    void add(Articles article);

    //查询文章列表——映射文件
    List<Articles> articleList(Integer parentColumnId, Integer subColumnId, String state);

    // 根据文章ID查询文章
    @Select("SELECT * FROM articles WHERE article_id = #{articleId}")
    Articles getArticleById(Integer articleId);

    @Update("UPDATE articles SET parent_column_id=#{parentColumnId}, sub_column_id=#{subColumnId}, " +
            "state = #{state}, title = #{title}, source = #{source}, " +
            "resume = #{resume}, cover_image_url = #{coverImageUrl}, video_url = #{videoUrl}, " +
            "content = #{content}, updated_time = #{updatedTime} " +
            "WHERE article_id = #{articleId}")
    void updateArticle(Articles article);

    @Delete("DELETE FROM articles WHERE article_id = #{articleId}")
    void deleteArticle(Integer articleId);

    //根据id直接找到
    List<Articles> articleListById(Integer parentColumnId, Integer subColumnId);
}
